/*
 * Daniel R Padilla
 *
 * Copyright (c) 2008, Daniel R Padilla
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.databasefacade.select;


import org.databasefacade.table.Table;
import org.databasefacade.translator.Translator;


public class Join
{
	public static final short ANSI_NOTATION = 1;
	public static final short NATIVE_NOTATION = 2;
	
	public static final short INNER = 10;
	public static final short LEFT_OUTER = 11;
	public static final short RIGHT_OUTER = 12;
	public static final short FULL_OUTER = 13;
	public static final short CROSS = 14;
	
	private short joinType;
	private Table table;
	private String alias;
	private QueryHints hints;
	private On onConditions;
	private Translator translator;
	
	public Join(short joinType, Table table, On on)
	{
		init(joinType, table, on, null);
		this.translator = table.getDBHelper().getTranslator();
	}
	
	public Join(short joinType, Table table, On on, QueryHints hints)
	{
		init(joinType, table, on, hints);
		this.translator = table.getDBHelper().getTranslator();
	}
	
	public On on()
	{
		return onConditions;
	}
	
	public Table getTable()
	{
		return table;
	}
	
	public int getJoinType()
	{
		return joinType;
	}
	
	public String getTableName()
	{
		return table.getTableName();
	}
	
	public String getTableAlias()
	{
		if ( alias.equals(getTableName()) ) {
			return "";
		} else {
			return alias;
		}
	}
	
	public QueryHints getHints()
	{
		return hints;
	}
	
	private void init(short joinType, Table table, On on, QueryHints hints)
	{
		this.joinType = joinType;
		this.table = table;
		this.alias = table.getTableAlias();
		this.onConditions = on;
		if ( hints == null ) {
			this.hints = hints;
		} else {
			this.hints = translator.getQueryHints();
		}
	}
	
	/**
	 * The method build the SQL statement that will be returned
	 * from the formation of the from clause. The statement build
	 * supports both comma delimited and ansi SQL JOIN notation.
	 * 
	 * @return
	 */
	public String getSQL()
	{
		String spacing = "\n       ";
		String sql = spacing
			+ translator.getAnsiJoinExtendedSQL(joinType, table.getTableName(), alias, hints);
		if ( onConditions != null ) {
				return sql + spacing + " " + onConditions.getSQL();
		} 
		return sql;
	}
}
